package com.doform.android.xianggucheng.activity;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.GuidBean;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/12.
 */

public class NewStarActivity extends BaseTitleActivity {

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_sort)
    EditText et_sort;

    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.tv_sort)
    TextView tv_sort;

    @Override
    protected int layoutResId() {
        return R.layout.activity_common_sort;
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle(getResources().getString(R.string.star_add_title));

        tv_name.setText(getResources().getString(R.string.star_tv_name));
        tv_sort.setText(getResources().getString(R.string.default_tv_sort));

        et_name.setHint(getResources().getString(R.string.star_name_hint));
        et_sort.setText(getResources().getString(R.string.default_et_sort));
    }

    private void beforeSubmit() {

        String mName = et_name.getText().toString().trim();
        String mSort = et_sort.getText().toString().trim();

        if (mName == null || TextUtils.isEmpty(mName)) {
            Toast.makeText(this, "请输入星级名称", Toast.LENGTH_SHORT).show();
            return;
        }
        postDataForService(mName, mSort);
    }

    private void postDataForService(String mName, String mSort) {
        HttpManager
                .instance()
                .addStar(mName, mSort)
                .subscribe(new ErrorHandleSubscriber<GuidBean>() {
                    @Override
                    public void onNext(@NonNull GuidBean i) {
                        finish();
                    }
                });
    }

    @OnClick(R.id.btn_submit)
    public void submit() {
        beforeSubmit();
    }

}
